package com.deram.ctin.serv.enter.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.deram.ctin.common.bean.PageResult;
import com.deram.ctin.common.consts.ErrorConsts;
import com.deram.ctin.common.error.BusinessException;
import com.deram.ctin.common.utils.ArrayHandleUtils;
import com.deram.ctin.common.utils.BusinessUtils;
import com.deram.ctin.common.utils.ValidateUtils;
import com.deram.ctin.dependencies.serv.utils.MybatisUtils;
import com.deram.ctin.serv.enter.entity.BaseUpdateInfo;
import com.deram.ctin.serv.enter.entity.EnterInfo;
import com.deram.ctin.serv.enter.entity.EnterPerson;
import com.deram.ctin.serv.enter.filter.EnterPersonFilter;
import com.deram.ctin.serv.enter.mapper.EnterPersonMapper;
import com.deram.ctin.serv.enter.service.EnterInfoService;
import com.deram.ctin.serv.enter.service.EnterPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: jtt
 * @Description:
 * @Date: 2017/9/28 17:34
 * @version: V1.0
 */
@Service
public class EnterPersonServiceImpl implements EnterPersonService{

    @Autowired
    EnterPersonMapper enterPersonMapper;

    @Autowired
    EnterInfoService enterInfoService;

    @Override
    public EnterPerson find(Long id) {
        ValidateUtils.isId(id, "自然人ID");
        EnterPerson result = enterPersonMapper.selectById(id);
        BusinessUtils.notNull(result, "自然人信息不存在");
        return result;
    }

    @Override
    public List<EnterPerson> list(EnterPersonFilter filter) {
        EntityWrapper<EnterPerson> ew = createEntityWrapper(filter);
        List<EnterPerson> result = enterPersonMapper.selectList(ew);
        return result;
    }

    @Override
    public EnterPerson save(EnterPerson param) {
        ValidateUtils.isId(param.getEnterId(), "企业ID");
        if (null != param.getId()) {
            EnterPerson finder = find(param.getId());
            enterPersonMapper.updateById(param);
        } else {
            enterPersonMapper.insert(param);
        }
        return param;
    }

    @Override
    public void delete(Long id) {
        enterPersonMapper.deleteById(find(id));
    }

    @Override
    public void deleteByEnterId(Long id) {
        EnterPersonFilter filter = new EnterPersonFilter();
        filter.setEnterId(id);
        List<EnterPerson> lists = list(filter);
        if (null != lists && !lists.isEmpty()) {
            List<Long> ids = ArrayHandleUtils.readAttrList(lists, "id");
            enterPersonMapper.deleteBatchIds(ids);
        }
    }

    @Override
    public void saveList(BaseUpdateInfo<EnterPerson> updateInfo) {
        Long enterId = updateInfo.getEnterId();
        EnterInfo enterInfo = enterInfoService.find(enterId);
        if (null == enterInfo) {
            throw new BusinessException(ErrorConsts.VALIDATE, "工商信息未找到");
        }
        deleteByEnterId(enterId);
        List<EnterPerson> lists = updateInfo.getList();
        for (EnterPerson item : lists) {
            item.setId(null);
            item.setEnterId(enterId);
        }
        enterPersonMapper.batchInsert(lists);
    }

    @Override
    public PageResult<EnterPerson> search(EnterPersonFilter filter, Integer pageNo, Integer pageSize) {
        Pagination pagination = MybatisUtils.createPagination(pageNo,pageSize);
        EntityWrapper<EnterPerson> entityWrapper = createEntityWrapper(filter);
        return MybatisUtils.createPageResult(enterPersonMapper.selectPage(pagination,entityWrapper),pagination);
    }

    private EntityWrapper<EnterPerson> createEntityWrapper(EnterPersonFilter filter) {
        EnterPerson entity = new EnterPerson();
        entity.setEnterId(filter.getEnterId());
        entity.setCode(filter.getCode());
        EntityWrapper<EnterPerson> wrapper = new EntityWrapper<>(entity);
        return wrapper;
    }
}
